﻿using System;
using System.Collections;

namespace _83_IEnumerable接口与foreach循环
{
    class Program
    {
        static void Main(string[] args)
        {
            MyExampleCollection 输出 = new MyExampleCollection();
            Console.WriteLine("枚举出四个字符串实例");
            foreach (var item in 输出)
            {
                Console.WriteLine(item);

            }
            Console.ReadLine();
        }
    }

    internal class MyEnumerator : IEnumerator
    {
        string[] _arr;
        int _currentIndex;

        public MyEnumerator(string[] src)
        {
            _arr = src;
            _currentIndex = -1;
        }

        public object Current { get; private set; }

        public bool MoveNext()
        {
            //如果当 前位置已超出索引最大值，就返回false
            if (++_currentIndex>=_arr.Length)
            {
                Current = null;
                return false;
            }

            Current = _arr[_currentIndex];
            return true;
        }

        public void Reset()
        {
            throw new NotImplementedException();
        }

    }

    //声明一个模拟类，实现IEnumerable方法
    public class MyExampleCollection : IEnumerable
    {
        string[] 模拟数据 = { "red", "blue", "green", "gray", "zoomla" };

        public IEnumerator GetEnumerator()
        {
            return new MyEnumerator(模拟数据);
        }
    }

}
